package jp.sourceforge.akjrcp.snack;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import jp.sourceforge.akjrcp.akj_betas.Akj_betasPlugin;
import jp.sourceforge.akjrcp.ffmpeg.FFMpegUtils;
import jp.sourceforge.akjrcp.ui.ProgressMonitor;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;




public class SpectrogramExecuter {
	public static Log log=LogFactory.getLog(SpectrogramExecuter.class);
	private String spectrogramExePath;
	private Process process;
	private boolean cancel;
	/*
	public String[] getCommands(String wavePath,int start,int end,int min,int max,double framelength){
		String argv[]={pitch2ExePath,wavePath,""+start,""+end,""+min,""+max,""+framelength};
		return argv;
	}
	*/
	
	public void cancel(){
		cancel=true;
		if(process!=null){
			process.destroy();
		}
	}

	public   void createSpectrogramImage(File audioFile[],String outputDir,int samplerate,int width,int height,double persample,int start,int end,ProgressMonitor monitor,SpectrogramListener listener) throws IOException{

	    
	    SpectrogramGifConverter parser=new SpectrogramGifConverter(samplerate,height,persample);
	    parser.setImageWidth(width);//very important
	    parser.setProgressMonitor(monitor);
	    parser.setSpectrogramListener(listener);
	    parser.setExportPath(outputDir);
	     
	    for(int i=0;i<audioFile.length;i++){
	     if(cancel){
	    	 return;
	     }
	     String argv[]={spectrogramExePath,audioFile[i].getAbsolutePath(),""+height,""+persample,""+start,""+end};
	     //TODO independent
	     Akj_betasPlugin.getDefault().getProcessPrinter().printOutput(FFMpegUtils.toPrintCommand(argv)+"\n");
	     log.trace("start:getSpectoram");
	     process = Runtime.getRuntime().exec(argv);
	     log.trace("end:getSpectoram");
	     if(process!=null){
	         InputStream input=null;
	         
	             if(process!=null){
	             input=process.getInputStream();
	             if(input!=null){
	                 long starttime=System.currentTimeMillis();
	                 log.trace("start-parse-spectrogram:");
	                 
	                 parser.parse(input);
	                 log.trace("end-parse-spectrogram:"+(System.currentTimeMillis()-starttime)+"ms");
	             }
	             
	         }
	     }
	     //monitor.worked(1);//this time call per file.
	    }
	     }


	public Spectrogram getSpectram(String path,int samplerate,int height,int persample,int start,int end) throws IOException{
	return getSpectram(path,samplerate,height,persample,start,end,null)  ;
	}
	public Spectrogram getSpectram(String path,int samplerate,int height,int persample,int start,int end,ProgressMonitor monitor) throws IOException{

	   
	    Spectrogram spectrogram=null;
	  
	    
	    String argv[]={spectrogramExePath,path,""+height,""+persample,""+start,""+end};
	    log.trace("start:getSpectoram");
	    process = Runtime.getRuntime().exec(argv);
	    log.trace("end:getSpectoram");
	    if(process!=null){
	        InputStream input=null;
	        synchronized(this){
	            if(process!=null){
	            input=process.getInputStream();
	            if(input!=null){
	                long starttime=System.currentTimeMillis();
	                log.trace("start-parse-spectrogram:");
	                SpectrogramParser parser=new SpectrogramParser(samplerate,height,persample);
	                parser.setProgressMonitor(monitor);
	                spectrogram=(Spectrogram)parser.parse(input);
	                log.trace("end-parse-spectrogram:"+(System.currentTimeMillis()-starttime)+"ms");
	            }
	            }
	        }
	    }
	    return spectrogram;
	    }



	public String getSpectrogramExePath() {
		return spectrogramExePath;
	}



	public void setSpectrogramExePath(String spectrogramExePath) {
		this.spectrogramExePath = spectrogramExePath;
	}


		
}


